********************************************************************************
**  TITLE:      ca2011_ipsos_invitation				                          **  
**  AUTHORS:	P. Mongrain, N. Fréchet, B. Thompson Collart, and Y. Dufresne **
**  DATA:       2011 Canada General Election Invitation Survey                **
**	DATE:		February 2025	 					                          **	
**  VERSION:	Stata 17					                                  **	
********************************************************************************

* Note: The data file needs to be downloaded from its original source (see the "surveys.text" file)

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "ca2011_ipsos_invitation", replace

* Open the dataset

import spss using "2011 Canada General Election Invitation Survey.sav", clear case(lower)

* Election

gen election = "ca2011"

* Type of survey

gen type = "invitation"

gen exit_survey = 0

* Respondent ID

gen responseid = respid

* Survey weight

merge 1:1 election responseid using ca2011_invitation_weight
drop _merge


***********************
** DISTRICT OUTCOMES **
***********************

decode seat, generate(seat_str)

gen str district_name = substr(seat_str,9,100)

gen str seat_code = substr(seat_str,1,5)
replace seat_code = "" if seat_str == "Not specified"
destring seat_code, gen(district_code)

* Election outcome in each district

gen district_outcome = .

replace district_outcome = 2 if district_code == 10001
replace district_outcome = 2 if district_code == 10002
replace district_outcome = 2 if district_code == 10003
replace district_outcome = 1 if district_code == 10004
replace district_outcome = 2 if district_code == 10005
replace district_outcome = 3 if district_code == 10006
replace district_outcome = 3 if district_code == 10007
replace district_outcome = 2 if district_code == 11001
replace district_outcome = 2 if district_code == 11002
replace district_outcome = 1 if district_code == 11003
replace district_outcome = 2 if district_code == 11004
replace district_outcome = 2 if district_code == 12001
replace district_outcome = 1 if district_code == 12002
replace district_outcome = 1 if district_code == 12007
replace district_outcome = 3 if district_code == 12003
replace district_outcome = 3 if district_code == 12004
replace district_outcome = 2 if district_code == 12005
replace district_outcome = 2 if district_code == 12006
replace district_outcome = 3 if district_code == 12008
replace district_outcome = 1 if district_code == 12009
replace district_outcome = 2 if district_code == 12010
replace district_outcome = 1 if district_code == 12011
replace district_outcome = 3 if district_code == 13001
replace district_outcome = 2 if district_code == 13002
replace district_outcome = 1 if district_code == 13003
replace district_outcome = 1 if district_code == 13004
replace district_outcome = 1 if district_code == 13005
replace district_outcome = 1 if district_code == 13006
replace district_outcome = 1 if district_code == 13007
replace district_outcome = 1 if district_code == 13008
replace district_outcome = 1 if district_code == 13009
replace district_outcome = 1 if district_code == 13010
replace district_outcome = 3 if district_code == 24046
replace district_outcome = 3 if district_code == 24001
replace district_outcome = 4 if district_code == 24002
replace district_outcome = 3 if district_code == 24003
replace district_outcome = 3 if district_code == 24004
replace district_outcome = 4 if district_code == 24054
replace district_outcome = 1 if district_code == 24005
replace district_outcome = 3 if district_code == 24006
replace district_outcome = 3 if district_code == 24007
replace district_outcome = 3 if district_code == 24008
replace district_outcome = 2 if district_code == 24009
replace district_outcome = 3 if district_code == 24010
replace district_outcome = 3 if district_code == 24011
replace district_outcome = 3 if district_code == 24012
replace district_outcome = 3 if district_code == 24013
replace district_outcome = 3 if district_code == 24015
replace district_outcome = 3 if district_code == 24016
replace district_outcome = 3 if district_code == 24017
replace district_outcome = 3 if district_code == 24018
replace district_outcome = 3 if district_code == 24019
replace district_outcome = 3 if district_code == 24020
replace district_outcome = 4 if district_code == 24041
replace district_outcome = 3 if district_code == 24021
replace district_outcome = 3 if district_code == 24022
replace district_outcome = 3 if district_code == 24023
replace district_outcome = 3 if district_code == 24024
replace district_outcome = 3 if district_code == 24025
replace district_outcome = 3 if district_code == 24026
replace district_outcome = 2 if district_code == 24027
replace district_outcome = 3 if district_code == 24028
replace district_outcome = 3 if district_code == 24029
replace district_outcome = 3 if district_code == 24030
replace district_outcome = 3 if district_code == 24031
replace district_outcome = 3 if district_code == 24032
replace district_outcome = 3 if district_code == 24033
replace district_outcome = 1 if district_code == 24034
replace district_outcome = 3 if district_code == 24035
replace district_outcome = 1 if district_code == 24036
replace district_outcome = 3 if district_code == 24037
replace district_outcome = 3 if district_code == 24038
replace district_outcome = 3 if district_code == 24039
replace district_outcome = 3 if district_code == 24040
replace district_outcome = 1 if district_code == 24042
replace district_outcome = 3 if district_code == 24043
replace district_outcome = 3 if district_code == 24058
replace district_outcome = 3 if district_code == 24014
replace district_outcome = 2 if district_code == 24044
replace district_outcome = 3 if district_code == 24045
replace district_outcome = 3 if district_code == 24047
replace district_outcome = 2 if district_code == 24048
replace district_outcome = 3 if district_code == 24049
replace district_outcome = 3 if district_code == 24050
replace district_outcome = 3 if district_code == 24051
replace district_outcome = 3 if district_code == 24052
replace district_outcome = 3 if district_code == 24053
replace district_outcome = 4 if district_code == 24055
replace district_outcome = 3 if district_code == 24056
replace district_outcome = 3 if district_code == 24057
replace district_outcome = 3 if district_code == 24059
replace district_outcome = 1 if district_code == 24060
replace district_outcome = 3 if district_code == 24061
replace district_outcome = 3 if district_code == 24062
replace district_outcome = 3 if district_code == 24063
replace district_outcome = 3 if district_code == 24064
replace district_outcome = 3 if district_code == 24065
replace district_outcome = 2 if district_code == 24066
replace district_outcome = 2 if district_code == 24067
replace district_outcome = 3 if district_code == 24068
replace district_outcome = 3 if district_code == 24069
replace district_outcome = 3 if district_code == 24070
replace district_outcome = 3 if district_code == 24071
replace district_outcome = 3 if district_code == 24072
replace district_outcome = 3 if district_code == 24073
replace district_outcome = 3 if district_code == 24074
replace district_outcome = 2 if district_code == 24075
replace district_outcome = 1 if district_code == 35001
replace district_outcome = 3 if district_code == 35002
replace district_outcome = 1 if district_code == 35003
replace district_outcome = 1 if district_code == 35004
replace district_outcome = 3 if district_code == 35005
replace district_outcome = 1 if district_code == 35006
replace district_outcome = 1 if district_code == 35007
replace district_outcome = 1 if district_code == 35008
replace district_outcome = 1 if district_code == 35009
replace district_outcome = 1 if district_code == 35026
replace district_outcome = 1 if district_code == 35010
replace district_outcome = 1 if district_code == 35011
replace district_outcome = 1 if district_code == 35012
replace district_outcome = 1 if district_code == 35013
replace district_outcome = 3 if district_code == 35015
replace district_outcome = 1 if district_code == 35016
replace district_outcome = 1 if district_code == 35017
replace district_outcome = 1 if district_code == 35018
replace district_outcome = 1 if district_code == 35014
replace district_outcome = 1 if district_code == 35019
replace district_outcome = 1 if district_code == 35020
replace district_outcome = 1 if district_code == 35021
replace district_outcome = 1 if district_code == 35022
replace district_outcome = 1 if district_code == 35023
replace district_outcome = 2 if district_code == 35024
replace district_outcome = 1 if district_code == 35025
replace district_outcome = 2 if district_code == 35027
replace district_outcome = 1 if district_code == 35028
replace district_outcome = 1 if district_code == 35029
replace district_outcome = 1 if district_code == 35030
replace district_outcome = 3 if district_code == 35031
replace district_outcome = 3 if district_code == 35032
replace district_outcome = 3 if district_code == 35033
replace district_outcome = 1 if district_code == 35034
replace district_outcome = 1 if district_code == 35035
replace district_outcome = 2 if district_code == 35036
replace district_outcome = 1 if district_code == 35037
replace district_outcome = 1 if district_code == 35038
replace district_outcome = 1 if district_code == 35039
replace district_outcome = 1 if district_code == 35046
replace district_outcome = 1 if district_code == 35040
replace district_outcome = 1 if district_code == 35041
replace district_outcome = 3 if district_code == 35042
replace district_outcome = 1 if district_code == 35043
replace district_outcome = 1 if district_code == 35044
replace district_outcome = 2 if district_code == 35045
replace district_outcome = 1 if district_code == 35047
replace district_outcome = 1 if district_code == 35048
replace district_outcome = 1 if district_code == 35049
replace district_outcome = 1 if district_code == 35050
replace district_outcome = 1 if district_code == 35051
replace district_outcome = 1 if district_code == 35052
replace district_outcome = 1 if district_code == 35053
replace district_outcome = 1 if district_code == 35054
replace district_outcome = 1 if district_code == 35055
replace district_outcome = 3 if district_code == 35056
replace district_outcome = 1 if district_code == 35057
replace district_outcome = 1 if district_code == 35058
replace district_outcome = 1 if district_code == 35059
replace district_outcome = 1 if district_code == 35060
replace district_outcome = 1 if district_code == 35061
replace district_outcome = 3 if district_code == 35062
replace district_outcome = 1 if district_code == 35063
replace district_outcome = 2 if district_code == 35064
replace district_outcome = 2 if district_code == 35065
replace district_outcome = 1 if district_code == 35066
replace district_outcome = 1 if district_code == 35067
replace district_outcome = 3 if district_code == 35068
replace district_outcome = 1 if district_code == 35069
replace district_outcome = 1 if district_code == 35070
replace district_outcome = 1 if district_code == 35071
replace district_outcome = 1 if district_code == 35072
replace district_outcome = 1 if district_code == 35073
replace district_outcome = 1 if district_code == 35074
replace district_outcome = 1 if district_code == 35075
replace district_outcome = 1 if district_code == 35076
replace district_outcome = 2 if district_code == 35077
replace district_outcome = 1 if district_code == 35078
replace district_outcome = 1 if district_code == 35079
replace district_outcome = 2 if district_code == 35080
replace district_outcome = 1 if district_code == 35081
replace district_outcome = 2 if district_code == 35082
replace district_outcome = 3 if district_code == 35083
replace district_outcome = 3 if district_code == 35084
replace district_outcome = 1 if district_code == 35085
replace district_outcome = 1 if district_code == 35086
replace district_outcome = 1 if district_code == 35087
replace district_outcome = 3 if district_code == 35088
replace district_outcome = 1 if district_code == 35089
replace district_outcome = 3 if district_code == 35090
replace district_outcome = 3 if district_code == 35091
replace district_outcome = 3 if district_code == 35092
replace district_outcome = 2 if district_code == 35093
replace district_outcome = 3 if district_code == 35094
replace district_outcome = 3 if district_code == 35095
replace district_outcome = 1 if district_code == 35096
replace district_outcome = 3 if district_code == 35097
replace district_outcome = 1 if district_code == 35098
replace district_outcome = 1 if district_code == 35099
replace district_outcome = 1 if district_code == 35100
replace district_outcome = 3 if district_code == 35101
replace district_outcome = 3 if district_code == 35102
replace district_outcome = 1 if district_code == 35103
replace district_outcome = 1 if district_code == 35104
replace district_outcome = 3 if district_code == 35105
replace district_outcome = 2 if district_code == 35106
replace district_outcome = 1 if district_code == 46001
replace district_outcome = 1 if district_code == 46002
replace district_outcome = 3 if district_code == 46003
replace district_outcome = 1 if district_code == 46004
replace district_outcome = 1 if district_code == 46005
replace district_outcome = 1 if district_code == 46006
replace district_outcome = 1 if district_code == 46007
replace district_outcome = 1 if district_code == 46008
replace district_outcome = 1 if district_code == 46009
replace district_outcome = 1 if district_code == 46010
replace district_outcome = 3 if district_code == 46011
replace district_outcome = 2 if district_code == 46012
replace district_outcome = 1 if district_code == 46013
replace district_outcome = 1 if district_code == 46014
replace district_outcome = 1 if district_code == 47001
replace district_outcome = 1 if district_code == 47002
replace district_outcome = 1 if district_code == 47004
replace district_outcome = 1 if district_code == 47003
replace district_outcome = 1 if district_code == 47005
replace district_outcome = 1 if district_code == 47006
replace district_outcome = 1 if district_code == 47007
replace district_outcome = 1 if district_code == 47008
replace district_outcome = 1 if district_code == 47009
replace district_outcome = 1 if district_code == 47010
replace district_outcome = 1 if district_code == 47011
replace district_outcome = 1 if district_code == 47012
replace district_outcome = 2 if district_code == 47013
replace district_outcome = 1 if district_code == 47014
replace district_outcome = 1 if district_code == 48006
replace district_outcome = 1 if district_code == 48003
replace district_outcome = 1 if district_code == 48002
replace district_outcome = 1 if district_code == 48004
replace district_outcome = 1 if district_code == 48005
replace district_outcome = 1 if district_code == 48007
replace district_outcome = 1 if district_code == 48008
replace district_outcome = 1 if district_code == 48009
replace district_outcome = 1 if district_code == 48010
replace district_outcome = 1 if district_code == 48012
replace district_outcome = 1 if district_code == 48013
replace district_outcome = 1 if district_code == 48014
replace district_outcome = 1 if district_code == 48011
replace district_outcome = 1 if district_code == 48015
replace district_outcome = 1 if district_code == 48016
replace district_outcome = 1 if district_code == 48017
replace district_outcome = 3 if district_code == 48018
replace district_outcome = 1 if district_code == 48001
replace district_outcome = 1 if district_code == 48019
replace district_outcome = 1 if district_code == 48020
replace district_outcome = 1 if district_code == 48021
replace district_outcome = 1 if district_code == 48022
replace district_outcome = 1 if district_code == 48023
replace district_outcome = 1 if district_code == 48024
replace district_outcome = 1 if district_code == 48025
replace district_outcome = 1 if district_code == 48026
replace district_outcome = 1 if district_code == 48027
replace district_outcome = 1 if district_code == 48028
replace district_outcome = 1 if district_code == 59001
replace district_outcome = 3 if district_code == 59026
replace district_outcome = 3 if district_code == 59002
replace district_outcome = 3 if district_code == 59003
replace district_outcome = 1 if district_code == 59004
replace district_outcome = 1 if district_code == 59005
replace district_outcome = 1 if district_code == 59006
replace district_outcome = 3 if district_code == 59008
replace district_outcome = 1 if district_code == 59009
replace district_outcome = 1 if district_code == 59010
replace district_outcome = 1 if district_code == 59011
replace district_outcome = 1 if district_code == 59012
replace district_outcome = 1 if district_code == 59013
replace district_outcome = 1 if district_code == 59014
replace district_outcome = 3 if district_code == 59015
replace district_outcome = 3 if district_code == 59016
replace district_outcome = 3 if district_code == 59017
replace district_outcome = 1 if district_code == 59019
replace district_outcome = 1 if district_code == 59020
replace district_outcome = 1 if district_code == 59018
replace district_outcome = 1 if district_code == 59007
replace district_outcome = 1 if district_code == 59021
replace district_outcome = 1 if district_code == 59022
replace district_outcome = 1 if district_code == 59023
replace district_outcome = 5 if district_code == 59024
replace district_outcome = 3 if district_code == 59025
replace district_outcome = 1 if district_code == 59027
replace district_outcome = 3 if district_code == 59028
replace district_outcome = 2 if district_code == 59029
replace district_outcome = 3 if district_code == 59030
replace district_outcome = 1 if district_code == 59031
replace district_outcome = 3 if district_code == 59032
replace district_outcome = 2 if district_code == 59033
replace district_outcome = 1 if district_code == 59034
replace district_outcome = 3 if district_code == 59035
replace district_outcome = 1 if district_code == 59036
replace district_outcome = 1 if district_code == 60001
replace district_outcome = 3 if district_code == 61001
replace district_outcome = 1 if district_code == 62001

label define outcome 1 "Conservative Party of Canada" 2 "Liberal Party of Canada" 3 "New Democratic Party of Canada" 4 "Bloc Québécois" 5 "Green Party of Canada" 88 "Other"
label values district_outcome outcome


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 3.5 if district_code == 10001
replace margin = 30.1 if district_code == 10002
replace margin = 31.8 if district_code == 10003
replace margin = .7000008 if district_code == 10004
replace margin = 17.7 if district_code == 10005
replace margin = 50.3 if district_code == 10006
replace margin = 19.4 if district_code == 10007
replace margin = 11.2 if district_code == 11001
replace margin = 6.799999 if district_code == 11002
replace margin = 23.3 if district_code == 11003
replace margin = 3.300003 if district_code == 11004
replace margin = 15.8 if district_code == 12001
replace margin = 32 if district_code == 12002
replace margin = 1.200001 if district_code == 12003
replace margin = 26 if district_code == 12004
replace margin = 5.400002 if district_code == 12005
replace margin = 3 if district_code == 12006
replace margin = 29.3 if district_code == 12007
replace margin = 23.6 if district_code == 12008
replace margin = 7 if district_code == 12009
replace margin = 2.100002 if district_code == 12010
replace margin = 10.6 if district_code == 12011
replace margin = 53.5 if district_code == 13001
replace margin = 5.799999 if district_code == 13002
replace margin = 24.6 if district_code == 13003
replace margin = 31.1 if district_code == 13004
replace margin = 5.399998 if district_code == 13005
replace margin = 29.3 if district_code == 13006
replace margin = 4.400002 if district_code == 13007
replace margin = 33.4 if district_code == 13008
replace margin = 19 if district_code == 13009
replace margin = 43.7 if district_code == 13010
replace margin = 19.6 if district_code == 24001
replace margin = 1.5 if district_code == 24002
replace margin = 19.3 if district_code == 24003
replace margin = 15.3 if district_code == 24004
replace margin = 20.7 if district_code == 24005
replace margin = 10.6 if district_code == 24006
replace margin = 19.9 if district_code == 24007
replace margin = 10.2 if district_code == 24008
replace margin = 8.600002 if district_code == 24009
replace margin = 20.5 if district_code == 24010
replace margin = 13.7 if district_code == 24011
replace margin = 15 if district_code == 24012
replace margin = 14.7 if district_code == 24013
replace margin = 2.399998 if district_code == 24014
replace margin = 25.3 if district_code == 24015
replace margin = 9.299999 if district_code == 24016
replace margin = 21.6 if district_code == 24017
replace margin = 29.6 if district_code == 24018
replace margin = 2.199999 if district_code == 24019
replace margin = 46.7 if district_code == 24020
replace margin = 17 if district_code == 24021
replace margin = 6.000002 if district_code == 24022
replace margin = 38.9 if district_code == 24023
replace margin = 20.5 if district_code == 24024
replace margin = 14.4 if district_code == 24025
replace margin = 8.200001 if district_code == 24026
replace margin = 4.099998 if district_code == 24027
replace margin = 15.8 if district_code == 24028
replace margin = 15.5 if district_code == 24029
replace margin = 12.3 if district_code == 24030
replace margin = 10.7 if district_code == 24031
replace margin = 20.6 if district_code == 24032
replace margin = 27 if district_code == 24033
replace margin = 10.1 if district_code == 24034
replace margin = 24.7 if district_code == 24035
replace margin = 1.400002 if district_code == 24036
replace margin = 14.5 if district_code == 24037
replace margin = 2.300003 if district_code == 24038
replace margin = 17.7 if district_code == 24039
replace margin = 23.3 if district_code == 24040
replace margin = 10.5 if district_code == 24041
replace margin = 22.9 if district_code == 24042
replace margin = 22.8 if district_code == 24043
replace margin = 5.800003 if district_code == 24044
replace margin = 7.800001 if district_code == 24045
replace margin = 22.1 if district_code == 24046
replace margin = 32.7 if district_code == 24047
replace margin = 10.1 if district_code == 24048
replace margin = 3.599998 if district_code == 24049
replace margin = 16.2 if district_code == 24050
replace margin = 14.9 if district_code == 24051
replace margin = 14.6 if district_code == 24052
replace margin = 20.8 if district_code == 24053
replace margin = 2.700001 if district_code == 24054
replace margin = 1.299999 if district_code == 24055
replace margin = 12.2 if district_code == 24056
replace margin = 20.7 if district_code == 24057
replace margin = .1000023 if district_code == 24058
replace margin = 27.1 if district_code == 24059
replace margin = 18 if district_code == 24060
replace margin = 18.2 if district_code == 24061
replace margin = 16.4 if district_code == 24062
replace margin = 27.8 if district_code == 24063
replace margin = 17 if district_code == 24064
replace margin = 16.7 if district_code == 24065
replace margin = 14.1 if district_code == 24066
replace margin = 10 if district_code == 24067
replace margin = 9.799999 if district_code == 24068
replace margin = 27.7 if district_code == 24069
replace margin = 7.199997 if district_code == 24070
replace margin = 18.5 if district_code == 24071
replace margin = 29.8 if district_code == 24072
replace margin = 17.9 if district_code == 24073
replace margin = 6.899998 if district_code == 24074
replace margin = 1.600002 if district_code == 24075
replace margin = 5.799999 if district_code == 35001
replace margin = 21.4 if district_code == 35002
replace margin = 26.6 if district_code == 35003
replace margin = 35.8 if district_code == 35004
replace margin = 10.8 if district_code == 35005
replace margin = .9000015 if district_code == 35006
replace margin = 20.4 if district_code == 35007
replace margin = 9.799999 if district_code == 35008
replace margin = 20.4 if district_code == 35009
replace margin = 30.9 if district_code == 35010
replace margin = 25.7 if district_code == 35011
replace margin = 33 if district_code == 35012
replace margin = 27.5 if district_code == 35013
replace margin = 33.4 if district_code == 35014
replace margin = 25.8 if district_code == 35015
replace margin = 2.200001 if district_code == 35016
replace margin = 1.100002 if district_code == 35017
replace margin = 44.3 if district_code == 35018
replace margin = 8.399998 if district_code == 35019
replace margin = 32.9 if district_code == 35020
replace margin = 12.9 if district_code == 35021
replace margin = 0 if district_code == 35022
replace margin = 5.200001 if district_code == 35023
replace margin = 10.3 if district_code == 35024
replace margin = 18.1 if district_code == 35025
replace margin = 38.7 if district_code == 35026
replace margin = 10.6 if district_code == 35027
replace margin = 26 if district_code == 35028
replace margin = 37.9 if district_code == 35029
replace margin = 28.7 if district_code == 35030
replace margin = 30.6 if district_code == 35031
replace margin = 9 if district_code == 35032
replace margin = 14.1 if district_code == 35033
replace margin = 29.6 if district_code == 35034
replace margin = 19.1 if district_code == 35035
replace margin = 4.399998 if district_code == 35036
replace margin = 11.1 if district_code == 35037
replace margin = 32.3 if district_code == 35038
replace margin = 3.300003 if district_code == 35039
replace margin = 36.6 if district_code == 35040
replace margin = 42.5 if district_code == 35041
replace margin = 17.4 if district_code == 35042
replace margin = 3.200001 if district_code == 35043
replace margin = 17.7 if district_code == 35044
replace margin = 3.400002 if district_code == 35045
replace margin = 33.7 if district_code == 35046
replace margin = 9.5 if district_code == 35047
replace margin = 1.5 if district_code == 35048
replace margin = 13.1 if district_code == 35049
replace margin = 9.299999 if district_code == 35050
replace margin = 6.900002 if district_code == 35051
replace margin = 29.3 if district_code == 35052
replace margin = 30.4 if district_code == 35053
replace margin = 29.8 if district_code == 35054
replace margin = 35.7 if district_code == 35055
replace margin = 27 if district_code == 35056
replace margin = .1000023 if district_code == 35057
replace margin = 32.8 if district_code == 35058
replace margin = 22.8 if district_code == 35059
replace margin = 20.9 if district_code == 35060
replace margin = 13.4 if district_code == 35061
replace margin = 30.4 if district_code == 35062
replace margin = 6.199997 if district_code == 35063
replace margin = 10.7 if district_code == 35064
replace margin = 8.800001 if district_code == 35065
replace margin = 13.2 if district_code == 35066
replace margin = 33.3 if district_code == 35067
replace margin = 14.3 if district_code == 35068
replace margin = 31.5 if district_code == 35069
replace margin = 33.2 if district_code == 35070
replace margin = 24.8 if district_code == 35071
replace margin = 2.5 if district_code == 35072
replace margin = 29.6 if district_code == 35073
replace margin = 34.7 if district_code == 35074
replace margin = 8.799999 if district_code == 35075
replace margin = 27.1 if district_code == 35076
replace margin = 8.199997 if district_code == 35077
replace margin = 22.7 if district_code == 35078
replace margin = 4.200001 if district_code == 35079
replace margin = 11.2 if district_code == 35080
replace margin = 3.899998 if district_code == 35081
replace margin = 1.799999 if district_code == 35082
replace margin = 10.7 if district_code == 35083
replace margin = 3.200001 if district_code == 35084
replace margin = 32 if district_code == 35085
replace margin = 34.6 if district_code == 35086
replace margin = 44.2 if district_code == 35087
replace margin = 21.6 if district_code == 35088
replace margin = 37.7 if district_code == 35089
replace margin = 21.5 if district_code == 35090
replace margin = 20.2 if district_code == 35091
replace margin = 18.7 if district_code == 35092
replace margin = 10.8 if district_code == 35093
replace margin = 43.2 if district_code == 35094
replace margin = 31.1 if district_code == 35095
replace margin = 26.4 if district_code == 35096
replace margin = 2 if district_code == 35097
replace margin = 47.3 if district_code == 35098
replace margin = 36.1 if district_code == 35099
replace margin = 1.799999 if district_code == 35100
replace margin = 16.3 if district_code == 35101
replace margin = 22.7 if district_code == 35102
replace margin = 15.2 if district_code == 35103
replace margin = 44.3 if district_code == 35104
replace margin = 7.299999 if district_code == 35105
replace margin = 19.2 if district_code == 35106
replace margin = 38.5 if district_code == 46001
replace margin = 37.5 if district_code == 46002
replace margin = 24.9 if district_code == 46003
replace margin = 37 if district_code == 46004
replace margin = .9000015 if district_code == 46005
replace margin = 28.1 if district_code == 46006
replace margin = 66.2 if district_code == 46007
replace margin = 52.7 if district_code == 46008
replace margin = 19.5 if district_code == 46009
replace margin = 38.7 if district_code == 46010
replace margin = 26.1 if district_code == 46011
replace margin = .2000008 if district_code == 46012
replace margin = 19.5 if district_code == 46013
replace margin = 1.799999 if district_code == 46014
replace margin = 39.8 if district_code == 47001
replace margin = 17.6 if district_code == 47002
replace margin = 3.600002 if district_code == 47003
replace margin = 48.6 if district_code == 47004
replace margin = 2.299999 if district_code == 47005
replace margin = 30.4 if district_code == 47006
replace margin = 16.4 if district_code == 47007
replace margin = 15.1 if district_code == 47008
replace margin = 17.6 if district_code == 47009
replace margin = 1.799999 if district_code == 47010
replace margin = 27 if district_code == 47011
replace margin = 55.3 if district_code == 47012
replace margin = 3.899998 if district_code == 47013
replace margin = 47.1 if district_code == 47014
replace margin = 58.6 if district_code == 48001
replace margin = 53.3 if district_code == 48002
replace margin = 40.5 if district_code == 48003
replace margin = 29.1 if district_code == 48004
replace margin = 57.7 if district_code == 48005
replace margin = 40.2 if district_code == 48006
replace margin = 66 if district_code == 48007
replace margin = 63.2 if district_code == 48008
replace margin = 44.5 if district_code == 48009
replace margin = 74.9 if district_code == 48010
replace margin = 37.2 if district_code == 48011
replace margin = 22.6 if district_code == 48012
replace margin = 15.3 if district_code == 48013
replace margin = 44.3 if district_code == 48014
replace margin = 42.1 if district_code == 48015
replace margin = 15.2 if district_code == 48016
replace margin = 55.3 if district_code == 48017
replace margin = 12.9 if district_code == 48018
replace margin = 29.3 if district_code == 48019
replace margin = 67.2 if district_code == 48020
replace margin = 58.4 if district_code == 48021
replace margin = 59.7 if district_code == 48022
replace margin = 60.8 if district_code == 48023
replace margin = 68.5 if district_code == 48024
replace margin = 65.60001 if district_code == 48025
replace margin = 70 if district_code == 48026
replace margin = 63.4 if district_code == 48027
replace margin = 63.9 if district_code == 48028
replace margin = 44.8 if district_code == 59001
replace margin = 2.099998 if district_code == 59002
replace margin = 13.9 if district_code == 59003
replace margin = 26 if district_code == 59004
replace margin = 31.4 if district_code == 59005
replace margin = 30.9 if district_code == 59006
replace margin = 18.8 if district_code == 59007
replace margin = .7000008 if district_code == 59008
replace margin = 14.7 if district_code == 59009
replace margin = 15.3 if district_code == 59010
replace margin = 35.3 if district_code == 59011
replace margin = 22.7 if district_code == 59012
replace margin = 44 if district_code == 59013
replace margin = 8.100002 if district_code == 59014
replace margin = 10.6 if district_code == 59015
replace margin = 1.900002 if district_code == 59016
replace margin = 4.400002 if district_code == 59017
replace margin = 29 if district_code == 59018
replace margin = 19 if district_code == 59019
replace margin = 29.5 if district_code == 59020
replace margin = 26 if district_code == 59021
replace margin = 36.5 if district_code == 59022
replace margin = 39.7 if district_code == 59023
replace margin = 10.6 if district_code == 59024
replace margin = 20.8 if district_code == 59025
replace margin = 12 if district_code == 59026
replace margin = 34.3 if district_code == 59027
replace margin = 4.100002 if district_code == 59028
replace margin = 5 if district_code == 59029
replace margin = 43.9 if district_code == 59030
replace margin = 3.099998 if district_code == 59031
replace margin = 22 if district_code == 59032
replace margin = 3.600002 if district_code == 59033
replace margin = 8.599998 if district_code == 59034
replace margin = 27.2 if district_code == 59035
replace margin = 21.9 if district_code == 59036
replace margin = .8999977 if district_code == 60001
replace margin = 13.7 if district_code == 61001
replace margin = 21.2 if district_code == 62001

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.64628 if district_code == 10001
replace enep = 2.335194 if district_code == 10002
replace enep = 2.41807 if district_code == 10003
replace enep = 2.851782 if district_code == 10004
replace enep = 2.637569 if district_code == 10005
replace enep = 1.800158 if district_code == 10006
replace enep = 2.757107 if district_code == 10007
replace enep = 2.474023 if district_code == 11001
replace enep = 3.062703 if district_code == 11002
replace enep = 2.428658 if district_code == 11003
replace enep = 2.812987 if district_code == 11004
replace enep = 2.86726 if district_code == 12001
replace enep = 2.454584 if district_code == 12002
replace enep = 3.145465 if district_code == 12003
replace enep = 2.731218 if district_code == 12004
replace enep = 3.229933 if district_code == 12005
replace enep = 3.01009 if district_code == 12006
replace enep = 2.738976 if district_code == 12007
replace enep = 2.499913 if district_code == 12008
replace enep = 2.889447 if district_code == 12009
replace enep = 2.948035 if district_code == 12010
replace enep = 2.689748 if district_code == 12011
replace enep = 1.879933 if district_code == 13001
replace enep = 3.121342 if district_code == 13002
replace enep = 2.887169 if district_code == 13003
replace enep = 2.364256 if district_code == 13004
replace enep = 3.069914 if district_code == 13005
replace enep = 2.650031 if district_code == 13006
replace enep = 3.225359 if district_code == 13007
replace enep = 2.529692 if district_code == 13008
replace enep = 2.717598 if district_code == 13009
replace enep = 2.19715 if district_code == 13010
replace enep = 2.663272 if district_code == 24001
replace enep = 3.605175 if district_code == 24002
replace enep = 3.443408 if district_code == 24003
replace enep = 3.24391 if district_code == 24004
replace enep = 2.748053 if district_code == 24005
replace enep = 3.068219 if district_code == 24006
replace enep = 3.098537 if district_code == 24007
replace enep = 3.527262 if district_code == 24008
replace enep = 3.272894 if district_code == 24009
replace enep = 3.444855 if district_code == 24010
replace enep = 3.456261 if district_code == 24011
replace enep = 3.495806 if district_code == 24012
replace enep = 3.073377 if district_code == 24013
replace enep = 3.263921 if district_code == 24014
replace enep = 2.772671 if district_code == 24015
replace enep = 3.38382 if district_code == 24016
replace enep = 3.092471 if district_code == 24017
replace enep = 2.878245 if district_code == 24018
replace enep = 3.752824 if district_code == 24019
replace enep = 2.321872 if district_code == 24020
replace enep = 2.887028 if district_code == 24021
replace enep = 3.638997 if district_code == 24022
replace enep = 2.442038 if district_code == 24023
replace enep = 3.28513 if district_code == 24024
replace enep = 2.883972 if district_code == 24025
replace enep = 2.893811 if district_code == 24026
replace enep = 3.444523 if district_code == 24027
replace enep = 2.827383 if district_code == 24028
replace enep = 3.479834 if district_code == 24029
replace enep = 3.159747 if district_code == 24030
replace enep = 2.794725 if district_code == 24031
replace enep = 3.454124 if district_code == 24032
replace enep = 3.209428 if district_code == 24033
replace enep = 3.005214 if district_code == 24034
replace enep = 2.769669 if district_code == 24035
replace enep = 3.007365 if district_code == 24036
replace enep = 3.648663 if district_code == 24037
replace enep = 3.070848 if district_code == 24038
replace enep = 2.828414 if district_code == 24039
replace enep = 2.931262 if district_code == 24040
replace enep = 3.779961 if district_code == 24041
replace enep = 2.927624 if district_code == 24042
replace enep = 2.61399 if district_code == 24043
replace enep = 3.017948 if district_code == 24044
replace enep = 3.445567 if district_code == 24045
replace enep = 3.364975 if district_code == 24046
replace enep = 2.569049 if district_code == 24047
replace enep = 3.364455 if district_code == 24048
replace enep = 3.503081 if district_code == 24049
replace enep = 3.100237 if district_code == 24050
replace enep = 3.261898 if district_code == 24051
replace enep = 3.331557 if district_code == 24052
replace enep = 2.645985 if district_code == 24053
replace enep = 3.317564 if district_code == 24054
replace enep = 3.493828 if district_code == 24055
replace enep = 3.21858 if district_code == 24056
replace enep = 2.9097 if district_code == 24057
replace enep = 3.244067 if district_code == 24058
replace enep = 2.521973 if district_code == 24059
replace enep = 3.009166 if district_code == 24060
replace enep = 2.644271 if district_code == 24061
replace enep = 3.232229 if district_code == 24062
replace enep = 2.754684 if district_code == 24063
replace enep = 2.954489 if district_code == 24064
replace enep = 3.375072 if district_code == 24065
replace enep = 3.219409 if district_code == 24066
replace enep = 3.2105 if district_code == 24067
replace enep = 3.513457 if district_code == 24068
replace enep = 2.890616 if district_code == 24069
replace enep = 3.004627 if district_code == 24070
replace enep = 2.825976 if district_code == 24071
replace enep = 2.74207 if district_code == 24072
replace enep = 3.364444 if district_code == 24073
replace enep = 2.968328 if district_code == 24074
replace enep = 3.331645 if district_code == 24075
replace enep = 2.750547 if district_code == 35001
replace enep = 2.618055 if district_code == 35002
replace enep = 2.767906 if district_code == 35003
replace enep = 2.535073 if district_code == 35004
replace enep = 3.109704 if district_code == 35005
replace enep = 3.203557 if district_code == 35006
replace enep = 2.848954 if district_code == 35007
replace enep = 2.817457 if district_code == 35008
replace enep = 2.803264 if district_code == 35009
replace enep = 2.599928 if district_code == 35010
replace enep = 2.590734 if district_code == 35011
replace enep = 2.464019 if district_code == 35012
replace enep = 2.584019 if district_code == 35013
replace enep = 2.655506 if district_code == 35014
replace enep = 2.580279 if district_code == 35015
replace enep = 3.130244 if district_code == 35016
replace enep = 2.677469 if district_code == 35017
replace enep = 2.473448 if district_code == 35018
replace enep = 2.624948 if district_code == 35019
replace enep = 2.438269 if district_code == 35020
replace enep = 2.659532 if district_code == 35021
replace enep = 2.764096 if district_code == 35022
replace enep = 3.044808 if district_code == 35023
replace enep = 2.949035 if district_code == 35024
replace enep = 2.76868 if district_code == 35025
replace enep = 2.605076 if district_code == 35026
replace enep = 3.052727 if district_code == 35027
replace enep = 2.760509 if district_code == 35028
replace enep = 2.335996 if district_code == 35029
replace enep = 2.561777 if district_code == 35030
replace enep = 2.41037 if district_code == 35031
replace enep = 2.826032 if district_code == 35032
replace enep = 2.782361 if district_code == 35033
replace enep = 2.542013 if district_code == 35034
replace enep = 2.87841 if district_code == 35035
replace enep = 3.084145 if district_code == 35036
replace enep = 3.067249 if district_code == 35037
replace enep = 2.621321 if district_code == 35038
replace enep = 2.971 if district_code == 35039
replace enep = 2.488237 if district_code == 35040
replace enep = 2.320541 if district_code == 35041
replace enep = 2.594141 if district_code == 35042
replace enep = 3.186236 if district_code == 35043
replace enep = 2.961559 if district_code == 35044
replace enep = 3.068247 if district_code == 35045
replace enep = 2.428092 if district_code == 35046
replace enep = 2.807743 if district_code == 35047
replace enep = 2.906318 if district_code == 35048
replace enep = 2.753888 if district_code == 35049
replace enep = 2.688389 if district_code == 35050
replace enep = 2.817362 if district_code == 35051
replace enep = 2.574307 if district_code == 35052
replace enep = 2.64759 if district_code == 35053
replace enep = 2.655471 if district_code == 35054
replace enep = 2.505399 if district_code == 35055
replace enep = 2.490251 if district_code == 35056
replace enep = 3.169482 if district_code == 35057
replace enep = 2.642581 if district_code == 35058
replace enep = 2.700849 if district_code == 35059
replace enep = 2.623329 if district_code == 35060
replace enep = 2.422715 if district_code == 35061
replace enep = 2.766481 if district_code == 35062
replace enep = 2.722 if district_code == 35063
replace enep = 2.953076 if district_code == 35064
replace enep = 3.241397 if district_code == 35065
replace enep = 2.946003 if district_code == 35066
replace enep = 2.36079 if district_code == 35067
replace enep = 2.805088 if district_code == 35068
replace enep = 2.573234 if district_code == 35069
replace enep = 2.65112 if district_code == 35070
replace enep = 2.808082 if district_code == 35071
replace enep = 2.957338 if district_code == 35072
replace enep = 2.656141 if district_code == 35073
replace enep = 2.820882 if district_code == 35074
replace enep = 2.865165 if district_code == 35075
replace enep = 2.780419 if district_code == 35076
replace enep = 3.122443 if district_code == 35077
replace enep = 2.587978 if district_code == 35078
replace enep = 2.890574 if district_code == 35079
replace enep = 2.806072 if district_code == 35080
replace enep = 3.139698 if district_code == 35081
replace enep = 3.123653 if district_code == 35082
replace enep = 3.044048 if district_code == 35083
replace enep = 3.23201 if district_code == 35084
replace enep = 3.207781 if district_code == 35085
replace enep = 2.65433 if district_code == 35086
replace enep = 2.228164 if district_code == 35087
replace enep = 2.759199 if district_code == 35088
replace enep = 2.230863 if district_code == 35089
replace enep = 2.786928 if district_code == 35090
replace enep = 2.731882 if district_code == 35091
replace enep = 2.634088 if district_code == 35092
replace enep = 3.195715 if district_code == 35093
replace enep = 2.35085 if district_code == 35094
replace enep = 2.617842 if district_code == 35095
replace enep = 2.379196 if district_code == 35096
replace enep = 2.778094 if district_code == 35097
replace enep = 2.204114 if district_code == 35098
replace enep = 2.420862 if district_code == 35099
replace enep = 2.725197 if district_code == 35100
replace enep = 2.635303 if district_code == 35101
replace enep = 2.454674 if district_code == 35102
replace enep = 2.688721 if district_code == 35103
replace enep = 2.191142 if district_code == 35104
replace enep = 3.046756 if district_code == 35105
replace enep = 2.878684 if district_code == 35106
replace enep = 2.103323 if district_code == 46001
replace enep = 2.453663 if district_code == 46002
replace enep = 2.688823 if district_code == 46003
replace enep = 2.116841 if district_code == 46004
replace enep = 2.348619 if district_code == 46005
replace enep = 2.289498 if district_code == 46006
replace enep = 1.68113 if district_code == 46007
replace enep = 1.865463 if district_code == 46008
replace enep = 2.671554 if district_code == 46009
replace enep = 2.004579 if district_code == 46010
replace enep = 2.618192 if district_code == 46011
replace enep = 3.077444 if district_code == 46012
replace enep = 2.520714 if district_code == 46013
replace enep = 3.045958 if district_code == 46014
replace enep = 1.912704 if district_code == 47001
replace enep = 2.294078 if district_code == 47002
replace enep = 2.330622 if district_code == 47003
replace enep = 1.863165 if district_code == 47004
replace enep = 2.356168 if district_code == 47005
replace enep = 2.041929 if district_code == 47006
replace enep = 2.355679 if district_code == 47007
replace enep = 2.289346 if district_code == 47008
replace enep = 2.4496 if district_code == 47009
replace enep = 2.182929 if district_code == 47010
replace enep = 2.245692 if district_code == 47011
replace enep = 1.708543 if district_code == 47012
replace enep = 2.9201 if district_code == 47013
replace enep = 1.895932 if district_code == 47014
replace enep = 1.83221 if district_code == 48001
replace enep = 2.034128 if district_code == 48002
replace enep = 2.620531 if district_code == 48003
replace enep = 2.42603 if district_code == 48004
replace enep = 1.905495 if district_code == 48005
replace enep = 2.528087 if district_code == 48006
replace enep = 1.66355 if district_code == 48007
replace enep = 1.706266 if district_code == 48008
replace enep = 2.283726 if district_code == 48009
replace enep = 1.397436 if district_code == 48010
replace enep = 2.262275 if district_code == 48011
replace enep = 2.88777 if district_code == 48012
replace enep = 2.36291 if district_code == 48013
replace enep = 2.178507 if district_code == 48014
replace enep = 2.162602 if district_code == 48015
replace enep = 3.177377 if district_code == 48016
replace enep = 1.849872 if district_code == 48017
replace enep = 2.210443 if district_code == 48018
replace enep = 2.478333 if district_code == 48019
replace enep = 1.62364 if district_code == 48020
replace enep = 1.848497 if district_code == 48021
replace enep = 1.65901 if district_code == 48022
replace enep = 1.658576 if district_code == 48023
replace enep = 1.530962 if district_code == 48024
replace enep = 1.598951 if district_code == 48025
replace enep = 1.474313 if district_code == 48026
replace enep = 1.723615 if district_code == 48027
replace enep = 1.629503 if district_code == 48028
replace enep = 2.105307 if district_code == 59001
replace enep = 2.731404 if district_code == 59002
replace enep = 2.584547 if district_code == 59003
replace enep = 2.417216 if district_code == 59004
replace enep = 2.448166 if district_code == 59005
replace enep = 2.638857 if district_code == 59006
replace enep = 2.346988 if district_code == 59007
replace enep = 2.891953 if district_code == 59008
replace enep = 2.780102 if district_code == 59009
replace enep = 2.414485 if district_code == 59010
replace enep = 2.502659 if district_code == 59011
replace enep = 2.337787 if district_code == 59012
replace enep = 2.131396 if district_code == 59013
replace enep = 2.685119 if district_code == 59014
replace enep = 2.53735 if district_code == 59015
replace enep = 3.227295 if district_code == 59016
replace enep = 2.555453 if district_code == 59017
replace enep = 2.540095 if district_code == 59018
replace enep = 2.83874 if district_code == 59019
replace enep = 2.729563 if district_code == 59020
replace enep = 2.411655 if district_code == 59021
replace enep = 2.185305 if district_code == 59022
replace enep = 2.428817 if district_code == 59023
replace enep = 2.780095 if district_code == 59024
replace enep = 2.336361 if district_code == 59025
replace enep = 2.404181 if district_code == 59026
replace enep = 2.699121 if district_code == 59027
replace enep = 3.127952 if district_code == 59028
replace enep = 3.9204 if district_code == 59029
replace enep = 2.243506 if district_code == 59030
replace enep = 2.482918 if district_code == 59031
replace enep = 2.784166 if district_code == 59032
replace enep = 2.865001 if district_code == 59033
replace enep = 2.901082 if district_code == 59034
replace enep = 2.883373 if district_code == 59035
replace enep = 3.140368 if district_code == 59036
replace enep = 3.584975 if district_code == 60001
replace enep = 2.87639 if district_code == 61001
replace enep = 2.707048 if district_code == 62001

* Competition measure

gen competition = .

replace competition = .2093095 if district_code == 10001
replace competition = .3375975 if district_code == 10002
replace competition = .360132 if district_code == 10003
replace competition = .2209675 if district_code == 10004
replace competition = .3125795 if district_code == 10005
replace competition = .3168695 if district_code == 10006
replace competition = .331666 if district_code == 10007
replace competition = .23736 if district_code == 11001
replace competition = .29672 if district_code == 11002
replace competition = .3160875 if district_code == 11003
replace competition = .2405195 if district_code == 11004
replace competition = .33458 if district_code == 12001
replace competition = .372246 if district_code == 12002
replace competition = .27063 if district_code == 12003
replace competition = .379438 if district_code == 12004
replace competition = .305096 if district_code == 12005
replace competition = .26775 if district_code == 12006
replace competition = .3996255 if district_code == 12007
replace competition = .334486 if district_code == 12008
replace competition = .279898 if district_code == 12009
replace competition = .2502635 if district_code == 12010
replace competition = .275618 if district_code == 12011
replace competition = .3351375 if district_code == 13001
replace competition = .30233 if district_code == 13002
replace competition = .384534 if district_code == 13003
replace competition = .3553295 if district_code == 13004
replace competition = .29773 if district_code == 13005
replace competition = .3740195 if district_code == 13006
replace competition = .297844 if district_code == 13007
replace competition = .394324 if district_code == 13008
replace competition = .338024 if district_code == 13009
replace competition = .3730035 if district_code == 13010
replace competition = .340124 if district_code == 24001
replace competition = .3358775 if district_code == 24002
replace competition = .4305375 if district_code == 24003
replace competition = .3977535 if district_code == 24004
replace competition = .3613855 if district_code == 24005
replace competition = .341226 if district_code == 24006
replace competition = .3972755 if district_code == 24007
replace competition = .396418 if district_code == 24008
replace competition = .353784 if district_code == 24009
replace competition = .4378635 if district_code == 24010
replace competition = .4066555 if district_code == 24011
replace competition = .428294 if district_code == 24012
replace competition = .3679455 if district_code == 24013
replace competition = .30421 if district_code == 24014
replace competition = .3929695 if district_code == 24015
replace competition = .3604495 if district_code == 24016
replace competition = .415092 if district_code == 24017
replace competition = .425836 if district_code == 24018
replace competition = .368314 if district_code == 24019
replace competition = .4079295 if district_code == 24020
replace competition = .359444 if district_code == 24021
replace competition = .3805901 if district_code == 24022
replace competition = .4087795 if district_code == 24023
replace competition = .4253015 if district_code == 24024
replace competition = .342602 if district_code == 24025
replace competition = .28822 if district_code == 24026
replace competition = .3305895 if district_code == 24027
replace competition = .342728 if district_code == 24028
replace competition = .4242295 if district_code == 24029
replace competition = .3676615 if district_code == 24030
replace competition = .2994534 if district_code == 24031
replace competition = .443742 if district_code == 24032
replace competition = .452538 if district_code == 24033
replace competition = .3253315 if district_code == 24034
replace competition = .3906095 if district_code == 24035
replace competition = .26227 if district_code == 24036
replace competition = .4253795 if district_code == 24037
replace competition = .2803515 if district_code == 24038
replace competition = .3539675 if district_code == 24039
replace competition = .4040735 if district_code == 24040
replace competition = .4185235 if district_code == 24041
replace competition = .3962935 if district_code == 24042
replace competition = .355694 if district_code == 24043
replace competition = .294408 if district_code == 24044
replace competition = .37372 if district_code == 24045
replace competition = .4441295 if district_code == 24046
replace competition = .4047295 if district_code == 24047
replace competition = .3608615 if district_code == 24048
replace competition = .338988 if district_code == 24049
replace competition = .383464 if district_code == 24050
replace competition = .3879175 if district_code == 24051
replace competition = .398974 if district_code == 24052
replace competition = .345228 if district_code == 24053
replace competition = .3206615 if district_code == 24054
replace competition = .3254655 if district_code == 24055
replace competition = .371966 if district_code == 24056
replace competition = .3868475 if district_code == 24057
replace competition = .2859815 if district_code == 24058
replace competition = .3633675 if district_code == 24059
replace competition = .369862 if district_code == 24060
replace competition = .328286 if district_code == 24061
replace competition = .401616 if district_code == 24062
replace competition = .399474 if district_code == 24063
replace competition = .369864 if district_code == 24064
replace competition = .4139275 if district_code == 24065
replace competition = .3820395 if district_code == 24066
replace competition = .356902 if district_code == 24067
replace competition = .38972 if district_code == 24068
replace competition = .4204835 if district_code == 24069
replace competition = .30829 if district_code == 24070
replace competition = .3587655 if district_code == 24071
replace competition = .413814 if district_code == 24072
replace competition = .4232195 if district_code == 24073
replace competition = .3003175 if district_code == 24074
replace competition = .312336 if district_code == 24075
replace competition = .248152 if district_code == 35001
replace competition = .336754 if district_code == 35002
replace competition = .388392 if district_code == 35003
replace competition = .401964 if district_code == 35004
replace competition = .335132 if district_code == 35005
replace competition = .2718255 if district_code == 35006
replace competition = .362758 if district_code == 35007
replace competition = .287746 if district_code == 35008
replace competition = .35598 if district_code == 35009
replace competition = .3830355 if district_code == 35010
replace competition = .3608055 if district_code == 35011
replace competition = .379668 if district_code == 35012
replace competition = .3665875 if district_code == 35013
replace competition = .40712 if district_code == 35014
replace competition = .359346 if district_code == 35015
replace competition = .274678 if district_code == 35016
replace competition = .1965475 if district_code == 35017
replace competition = .4209875 if district_code == 35018
replace competition = .2466 if district_code == 35019
replace competition = .3753755 if district_code == 35020
replace competition = .2858895 if district_code == 35021
replace competition = .203924 if district_code == 35022
replace competition = .28961 if district_code == 35023
replace competition = .3035596 if district_code == 35024
replace competition = .3390415 if district_code == 35025
replace competition = .4218215 if district_code == 35026
replace competition = .334942 if district_code == 35027
replace competition = .380858 if district_code == 35028
replace competition = .3825135 if district_code == 35029
replace competition = .3706575 if district_code == 35030
replace competition = .356234 if district_code == 35031
replace competition = .290852 if district_code == 35032
replace competition = .3163135 if district_code == 35033
replace competition = .370216 if district_code == 35034
replace competition = .3542315 if district_code == 35035
replace competition = .287452 if district_code == 35036
replace competition = .3338415 if district_code == 35037
replace competition = .3897235 if district_code == 35038
replace competition = .2693435 if district_code == 35039
replace competition = .396898 if district_code == 35040
replace competition = .3916475 if district_code == 35041
replace competition = .310056 if district_code == 35042
replace competition = .291588 if district_code == 35043
replace competition = .3485655 if district_code == 35044
replace competition = .277226 if district_code == 35045
replace competition = .3768815 if district_code == 35046
replace competition = .2832895 if district_code == 35047
replace competition = .2392315 if district_code == 35048
replace competition = .3034735 if district_code == 35049
replace competition = .2656855 if district_code == 35050
replace competition = .2686775 if district_code == 35051
replace competition = .3757175 if district_code == 35052
replace competition = .397086 if district_code == 35053
replace competition = .387614 if district_code == 35054
replace competition = .4001575 if district_code == 35055
replace competition = .350418 if district_code == 35056
replace competition = .2728595 if district_code == 35057
replace competition = .39426 if district_code == 35058
replace competition = .358742 if district_code == 35059
replace competition = .3357815 if district_code == 35060
replace competition = .24584 if district_code == 35061
replace competition = .404734 if district_code == 35062
replace competition = .247156 if district_code == 35063
replace competition = .3177155 if district_code == 35064
replace competition = .333958 if district_code == 35065
replace competition = .33002 if district_code == 35066
replace competition = .3677235 if district_code == 35067
replace competition = .3228535 if district_code == 35068
replace competition = .3941535 if district_code == 35069
replace competition = .40654 if district_code == 35070
replace competition = .379036 if district_code == 35071
replace competition = .2565055 if district_code == 35072
replace competition = .386398 if district_code == 35073
replace competition = .4396095 if district_code == 35074
replace competition = .29005 if district_code == 35075
replace competition = .3886255 if district_code == 35076
replace competition = .320786 if district_code == 35077
replace competition = .3422835 if district_code == 35078
replace competition = .257006 if district_code == 35079
replace competition = .29522 if district_code == 35080
replace competition = .2792075 if district_code == 35081
replace competition = .26865 if district_code == 35082
replace competition = .3144275 if district_code == 35083
replace competition = .290678 if district_code == 35084
replace competition = .479388 if district_code == 35085
replace competition = .409706 if district_code == 35086
replace competition = .376 if district_code == 35087
replace competition = .358826 if district_code == 35088
replace competition = .3619135 if district_code == 35089
replace competition = .3540895 if district_code == 35090
replace competition = .347504 if district_code == 35091
replace competition = .3207715 if district_code == 35092
replace competition = .34753 if district_code == 35093
replace competition = .40326 if district_code == 35094
replace competition = .3922015 if district_code == 35095
replace competition = .32747 if district_code == 35096
replace competition = .22305 if district_code == 35097
replace competition = .3927795 if district_code == 35098
replace competition = .3872995 if district_code == 35099
replace competition = .205724 if district_code == 35100
replace competition = .3072915 if district_code == 35101
replace competition = .3196615 if district_code == 35102
replace competition = .30605 if district_code == 35103
replace competition = .3846015 if district_code == 35104
replace competition = .2980015 if district_code == 35105
replace competition = .353606 if district_code == 35106
replace competition = .3420495 if district_code == 46001
replace competition = .3896315 if district_code == 46002
replace competition = .3609215 if district_code == 46003
replace competition = .337068 if district_code == 46004
replace competition = .1194875 if district_code == 46005
replace competition = .3239135 if district_code == 46006
replace competition = .317402 if district_code == 46007
replace competition = .3433875 if district_code == 46008
replace competition = .3328775 if district_code == 46009
replace competition = .3191075 if district_code == 46010
replace competition = .3732915 if district_code == 46011
replace competition = .250436 if district_code == 46012
replace competition = .3070295 if district_code == 46013
replace competition = .265318 if district_code == 46014
replace competition = .30028 if district_code == 47001
replace competition = .247574 if district_code == 47002
replace competition = .139094 if district_code == 47003
replace competition = .326868 if district_code == 47004
replace competition = .1341995 if district_code == 47005
replace competition = .279094 if district_code == 47006
replace competition = .253588 if district_code == 47007
replace competition = .2279375 if district_code == 47008
replace competition = .28451 if district_code == 47009
replace competition = .079976 if district_code == 47010
replace competition = .305068 if district_code == 47011
replace competition = .3082075 if district_code == 47012
replace competition = .2587595 if district_code == 47013
replace competition = .3291315 if district_code == 47014
replace competition = .341772 if district_code == 48001
replace competition = .3747855 if district_code == 48002
replace competition = .4307335 if district_code == 48003
replace competition = .3560155 if district_code == 48004
replace competition = .3560475 if district_code == 48005
replace competition = .417944 if district_code == 48006
replace competition = .31329 if district_code == 48007
replace competition = .32014 if district_code == 48008
replace competition = .4004935 if district_code == 48009
replace competition = .2340075 if district_code == 48010
replace competition = .365942 if district_code == 48011
replace competition = .375686 if district_code == 48012
replace competition = .2458395 if district_code == 48013
replace competition = .3783075 if district_code == 48014
replace competition = .3685175 if district_code == 48015
replace competition = .388616 if district_code == 48016
replace competition = .3424815 if district_code == 48017
replace competition = .1888335 if district_code == 48018
replace competition = .3710315 if district_code == 48019
replace competition = .304658 if district_code == 48020
replace competition = .346962 if district_code == 48021
replace competition = .3048855 if district_code == 48022
replace competition = .305476 if district_code == 48023
replace competition = .2771275 if district_code == 48024
replace competition = .29458 if district_code == 48025
replace competition = .259628 if district_code == 48026
replace competition = .324688 if district_code == 48027
replace competition = .3037715 if district_code == 48028
replace competition = .368668 if district_code == 59001
replace competition = .2178555 if district_code == 59002
replace competition = .2840275 if district_code == 59003
replace competition = .33949 if district_code == 59004
replace competition = .374132 if district_code == 59005
replace competition = .3943835 if district_code == 59006
replace competition = .27087 if district_code == 59007
replace competition = .2364635 if district_code == 59008
replace competition = .3185475 if district_code == 59009
replace competition = .2584295 if district_code == 59010
replace competition = .3989975 if district_code == 59011
replace competition = .2991875 if district_code == 59012
replace competition = .371898 if district_code == 59013
replace competition = .2579655 if district_code == 59014
replace competition = .2484 if district_code == 59015
replace competition = .2797175 if district_code == 59016
replace competition = .200754 if district_code == 59017
replace competition = .37541 if district_code == 59018
replace competition = .357804 if district_code == 59019
replace competition = .4088615 if district_code == 59020
replace competition = .336336 if district_code == 59021
replace competition = .3509655 if district_code == 59022
replace competition = .3953615 if district_code == 59023
replace competition = .29372 if district_code == 59024
replace competition = .284954 if district_code == 59025
replace competition = .230078 if district_code == 59026
replace competition = .4229255 if district_code == 59027
replace competition = .2984815 if district_code == 59028
replace competition = .393798 if district_code == 59029
replace competition = .3914455 if district_code == 59030
replace competition = .1735575 if district_code == 59031
replace competition = .368454 if district_code == 59032
replace competition = .25344 if district_code == 59033
replace competition = .292882 if district_code == 59034
replace competition = .413504 if district_code == 59035
replace competition = .4077015 if district_code == 59036
replace competition = .3370635 if district_code == 60001
replace competition = .3253275 if district_code == 61001
replace competition = .343718 if district_code == 62001


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if district_code == 10001
replace reelected = 1 if district_code == 10002
replace reelected = 1 if district_code == 10003
replace reelected = 0 if district_code == 10004
replace reelected = 1 if district_code == 10005
replace reelected = 1 if district_code == 10006
replace reelected = 0 if district_code == 10007
replace reelected = 1 if district_code == 11001
replace reelected = 1 if district_code == 11002
replace reelected = 1 if district_code == 11003
replace reelected = 1 if district_code == 11004
replace reelected = 1 if district_code == 12001
replace reelected = 1 if district_code == 12002
replace reelected = 0 if district_code == 12003
replace reelected = 1 if district_code == 12004
replace reelected = 1 if district_code == 12005
replace reelected = 1 if district_code == 12006
replace reelected = 0 if district_code == 12007
replace reelected = 1 if district_code == 12008
replace reelected = 1 if district_code == 12009
replace reelected = 1 if district_code == 12010
replace reelected = 1 if district_code == 12011
replace reelected = 1 if district_code == 13001
replace reelected = 1 if district_code == 13002
replace reelected = 1 if district_code == 13003
replace reelected = 1 if district_code == 13004
replace reelected = 0 if district_code == 13005
replace reelected = 1 if district_code == 13006
replace reelected = 0 if district_code == 13007
replace reelected = 1 if district_code == 13008
replace reelected = 1 if district_code == 13009
replace reelected = 1 if district_code == 13010
replace reelected = 0 if district_code == 24001
replace reelected = 1 if district_code == 24002
replace reelected = 0 if district_code == 24003
replace reelected = 0 if district_code == 24004
replace reelected = 1 if district_code == 24005
replace reelected = 0 if district_code == 24006
replace reelected = 0 if district_code == 24007
replace reelected = 0 if district_code == 24008
replace reelected = 1 if district_code == 24009
replace reelected = 0 if district_code == 24010
replace reelected = 0 if district_code == 24011
replace reelected = 0 if district_code == 24012
replace reelected = 0 if district_code == 24013
replace reelected = 0 if district_code == 24014
replace reelected = 0 if district_code == 24015
replace reelected = 0 if district_code == 24016
replace reelected = 0 if district_code == 24017
replace reelected = 0 if district_code == 24018
replace reelected = 0 if district_code == 24019
replace reelected = 0 if district_code == 24020
replace reelected = 0 if district_code == 24021
replace reelected = 0 if district_code == 24022
replace reelected = 0 if district_code == 24023
replace reelected = 0 if district_code == 24024
replace reelected = 0 if district_code == 24025
replace reelected = 0 if district_code == 24026
replace reelected = 1 if district_code == 24027
replace reelected = 0 if district_code == 24028
replace reelected = 0 if district_code == 24029
replace reelected = 0 if district_code == 24030
replace reelected = 0 if district_code == 24031
replace reelected = 0 if district_code == 24032
replace reelected = 0 if district_code == 24033
replace reelected = 1 if district_code == 24034
replace reelected = 0 if district_code == 24035
replace reelected = 1 if district_code == 24036
replace reelected = 0 if district_code == 24037
replace reelected = 0 if district_code == 24038
replace reelected = 0 if district_code == 24039
replace reelected = 0 if district_code == 24040
replace reelected = 1 if district_code == 24041
replace reelected = 1 if district_code == 24042
replace reelected = 0 if district_code == 24043
replace reelected = 1 if district_code == 24044
replace reelected = 0 if district_code == 24045
replace reelected = 0 if district_code == 24046
replace reelected = 1 if district_code == 24047
replace reelected = 1 if district_code == 24048
replace reelected = 0 if district_code == 24049
replace reelected = 0 if district_code == 24050
replace reelected = 0 if district_code == 24051
replace reelected = 0 if district_code == 24052
replace reelected = 0 if district_code == 24053
replace reelected = 1 if district_code == 24054
replace reelected = 1 if district_code == 24055
replace reelected = 0 if district_code == 24056
replace reelected = 0 if district_code == 24057
replace reelected = 0 if district_code == 24058
replace reelected = 0 if district_code == 24059
replace reelected = 1 if district_code == 24060
replace reelected = 0 if district_code == 24061
replace reelected = 0 if district_code == 24062
replace reelected = 0 if district_code == 24063
replace reelected = 0 if district_code == 24064
replace reelected = 0 if district_code == 24065
replace reelected = 1 if district_code == 24066
replace reelected = 1 if district_code == 24067
replace reelected = 0 if district_code == 24068
replace reelected = 0 if district_code == 24069
replace reelected = 0 if district_code == 24070
replace reelected = 0 if district_code == 24071
replace reelected = 0 if district_code == 24072
replace reelected = 0 if district_code == 24073
replace reelected = 0 if district_code == 24074
replace reelected = 1 if district_code == 24075
replace reelected = 0 if district_code == 35001
replace reelected = 1 if district_code == 35002
replace reelected = 1 if district_code == 35003
replace reelected = 1 if district_code == 35004
replace reelected = 0 if district_code == 35005
replace reelected = 0 if district_code == 35006
replace reelected = 0 if district_code == 35007
replace reelected = 0 if district_code == 35008
replace reelected = 1 if district_code == 35009
replace reelected = 1 if district_code == 35010
replace reelected = 1 if district_code == 35011
replace reelected = 1 if district_code == 35012
replace reelected = 1 if district_code == 35013
replace reelected = 1 if district_code == 35014
replace reelected = 0 if district_code == 35015
replace reelected = 0 if district_code == 35016
replace reelected = 0 if district_code == 35017
replace reelected = 1 if district_code == 35018
replace reelected = 0 if district_code == 35019
replace reelected = 1 if district_code == 35020
replace reelected = 1 if district_code == 35021
replace reelected = 0 if district_code == 35022
replace reelected = 0 if district_code == 35023
replace reelected = 1 if district_code == 35024
replace reelected = 1 if district_code == 35025
replace reelected = 1 if district_code == 35026
replace reelected = 1 if district_code == 35027
replace reelected = 1 if district_code == 35028
replace reelected = 1 if district_code == 35029
replace reelected = 1 if district_code == 35030
replace reelected = 1 if district_code == 35031
replace reelected = 1 if district_code == 35032
replace reelected = 1 if district_code == 35033
replace reelected = 1 if district_code == 35034
replace reelected = 1 if district_code == 35035
replace reelected = 1 if district_code == 35036
replace reelected = 1 if district_code == 35037
replace reelected = 1 if district_code == 35038
replace reelected = 1 if district_code == 35039
replace reelected = 1 if district_code == 35040
replace reelected = 1 if district_code == 35041
replace reelected = 1 if district_code == 35042
replace reelected = 0 if district_code == 35043
replace reelected = 1 if district_code == 35044
replace reelected = 1 if district_code == 35045
replace reelected = 1 if district_code == 35046
replace reelected = 0 if district_code == 35047
replace reelected = 0 if district_code == 35048
replace reelected = 1 if district_code == 35049
replace reelected = 0 if district_code == 35050
replace reelected = 0 if district_code == 35051
replace reelected = 1 if district_code == 35052
replace reelected = 1 if district_code == 35053
replace reelected = 1 if district_code == 35054
replace reelected = 1 if district_code == 35055
replace reelected = 1 if district_code == 35056
replace reelected = 0 if district_code == 35057
replace reelected = 1 if district_code == 35058
replace reelected = 1 if district_code == 35059
replace reelected = 1 if district_code == 35060
replace reelected = 1 if district_code == 35061
replace reelected = 1 if district_code == 35062
replace reelected = 1 if district_code == 35063
replace reelected = 1 if district_code == 35064
replace reelected = 1 if district_code == 35065
replace reelected = 1 if district_code == 35066
replace reelected = 1 if district_code == 35067
replace reelected = 0 if district_code == 35068
replace reelected = 1 if district_code == 35069
replace reelected = 1 if district_code == 35070
replace reelected = 1 if district_code == 35071
replace reelected = 0 if district_code == 35072
replace reelected = 1 if district_code == 35073
replace reelected = 1 if district_code == 35074
replace reelected = 0 if district_code == 35075
replace reelected = 1 if district_code == 35076
replace reelected = 1 if district_code == 35077
replace reelected = 1 if district_code == 35078
replace reelected = 0 if district_code == 35079
replace reelected = 1 if district_code == 35080
replace reelected = 0 if district_code == 35081
replace reelected = 1 if district_code == 35082
replace reelected = 0 if district_code == 35083
replace reelected = 0 if district_code == 35084
replace reelected = 1 if district_code == 35085
replace reelected = 1 if district_code == 35086
replace reelected = 1 if district_code == 35087
replace reelected = 1 if district_code == 35088
replace reelected = 1 if district_code == 35089
replace reelected = 1 if district_code == 35090
replace reelected = 1 if district_code == 35091
replace reelected = 1 if district_code == 35092
replace reelected = 1 if district_code == 35093
replace reelected = 1 if district_code == 35094
replace reelected = 1 if district_code == 35095
replace reelected = 0 if district_code == 35096
replace reelected = 1 if district_code == 35097
replace reelected = 1 if district_code == 35098
replace reelected = 1 if district_code == 35099
replace reelected = 0 if district_code == 35100
replace reelected = 1 if district_code == 35101
replace reelected = 1 if district_code == 35102
replace reelected = 0 if district_code == 35103
replace reelected = 1 if district_code == 35104
replace reelected = 0 if district_code == 35105
replace reelected = 1 if district_code == 35106
replace reelected = 1 if district_code == 46001
replace reelected = 1 if district_code == 46002
replace reelected = 1 if district_code == 46003
replace reelected = 1 if district_code == 46004
replace reelected = 0 if district_code == 46005
replace reelected = 1 if district_code == 46006
replace reelected = 1 if district_code == 46007
replace reelected = 1 if district_code == 46008
replace reelected = 1 if district_code == 46009
replace reelected = 1 if district_code == 46010
replace reelected = 1 if district_code == 46011
replace reelected = 0 if district_code == 46012
replace reelected = 1 if district_code == 46013
replace reelected = 0 if district_code == 46014
replace reelected = 1 if district_code == 47001
replace reelected = 1 if district_code == 47002
replace reelected = 1 if district_code == 47003
replace reelected = 1 if district_code == 47004
replace reelected = 1 if district_code == 47005
replace reelected = 1 if district_code == 47006
replace reelected = 1 if district_code == 47007
replace reelected = 1 if district_code == 47008
replace reelected = 1 if district_code == 47009
replace reelected = 1 if district_code == 47010
replace reelected = 1 if district_code == 47011
replace reelected = 1 if district_code == 47012
replace reelected = 1 if district_code == 47013
replace reelected = 1 if district_code == 47014
replace reelected = 1 if district_code == 48001
replace reelected = 1 if district_code == 48002
replace reelected = 1 if district_code == 48003
replace reelected = 1 if district_code == 48004
replace reelected = 1 if district_code == 48005
replace reelected = 1 if district_code == 48006
replace reelected = 1 if district_code == 48007
replace reelected = 1 if district_code == 48008
replace reelected = 1 if district_code == 48009
replace reelected = 1 if district_code == 48010
replace reelected = 1 if district_code == 48011
replace reelected = 1 if district_code == 48012
replace reelected = 1 if district_code == 48013
replace reelected = 1 if district_code == 48014
replace reelected = 1 if district_code == 48015
replace reelected = 1 if district_code == 48016
replace reelected = 1 if district_code == 48017
replace reelected = 1 if district_code == 48018
replace reelected = 1 if district_code == 48019
replace reelected = 1 if district_code == 48020
replace reelected = 1 if district_code == 48021
replace reelected = 1 if district_code == 48022
replace reelected = 1 if district_code == 48023
replace reelected = 1 if district_code == 48024
replace reelected = 1 if district_code == 48025
replace reelected = 1 if district_code == 48026
replace reelected = 1 if district_code == 48027
replace reelected = 1 if district_code == 48028
replace reelected = 1 if district_code == 59001
replace reelected = 1 if district_code == 59002
replace reelected = 1 if district_code == 59003
replace reelected = 1 if district_code == 59004
replace reelected = 1 if district_code == 59005
replace reelected = 1 if district_code == 59006
replace reelected = 1 if district_code == 59007
replace reelected = 0 if district_code == 59008
replace reelected = 1 if district_code == 59009
replace reelected = 1 if district_code == 59010
replace reelected = 1 if district_code == 59011
replace reelected = 1 if district_code == 59012
replace reelected = 1 if district_code == 59013
replace reelected = 1 if district_code == 59014
replace reelected = 1 if district_code == 59015
replace reelected = 0 if district_code == 59016
replace reelected = 1 if district_code == 59017
replace reelected = 1 if district_code == 59018
replace reelected = 1 if district_code == 59019
replace reelected = 1 if district_code == 59020
replace reelected = 1 if district_code == 59021
replace reelected = 1 if district_code == 59022
replace reelected = 1 if district_code == 59023
replace reelected = 0 if district_code == 59024
replace reelected = 1 if district_code == 59025
replace reelected = 1 if district_code == 59026
replace reelected = 1 if district_code == 59027
replace reelected = 0 if district_code == 59028
replace reelected = 1 if district_code == 59029
replace reelected = 1 if district_code == 59030
replace reelected = 1 if district_code == 59031
replace reelected = 1 if district_code == 59032
replace reelected = 1 if district_code == 59033
replace reelected = 0 if district_code == 59034
replace reelected = 1 if district_code == 59035
replace reelected = 1 if district_code == 59036
replace reelected = 0 if district_code == 60001
replace reelected = 1 if district_code == 61001
replace reelected = 1 if district_code == 62001

label define reelected 0 "Not reelected" 1 "Reelected"
label values reelected reelected


***************************
** INDEPENDENT VARIABLES **
***************************

* Province

gen province = prov

* Language

gen language = lang

recode language 1=0 2=1

label define language 0 "English" 1 "French"
label values language language

* Gender

gen male = gender

recode male 2=0

label define gender 0 "Female" 1 "Male"
label values male gender

* Age

gen r_age = ageval

replace r_age = . if r_age < 17

* Age 55 and over

gen age55 = 1 if r_age >= 55
replace age55 = 0 if r_age < 55 & r_age!=.

label define age55 0 "Less than 55" 1 "55 and over"
label values age55 age55

* Education

gen education = educ

label define education 1 "Primary school or less" 2 "Some high school" 3 "High school" 4 "Some community college/CEGEP/Trade school" 5 "Community college/CEGEP/Trade school" 6 "Some university" 7 "University undergraduate degree" 8 "University graduate degree"
label values education education

* University degree

gen univ = 1 if education >= 7
replace univ = 0 if education < 7 & education!=.

label define univ 0 "No university degree" 1 "University degree"
label values univ univ

* Postgraduate degree

gen postgrad = 1 if education == 8
replace postgrad = 0 if education < 8 & education!=.

label define postgrad 0 "No postgraduate degree" 1 "Postgraduate degree"
label values postgrad postgrad

* Postsecondary degree

gen postsec = 1 if education >= 5
replace postsec = 0 if education < 5 & education!=.

label define postsec 0 "No postsecondary" 1 "Postsecondary"
label values postsec postsec

* Immigrant

gen immigrant = q44

recode immigrant 1=0 2=1

label define immigrant 0 "Born in Canada" 1 "Immigrant"
label values immigrant immigrant

* Household income

gen income = hhincome

recode income 2=1 3=1 4=2 5=3 6=4 7=5 8=6 9=7 10=8 11=9 12=9 13=9 14=10 15=11 16=11 17=11 18=12 19=12 20=12 21=13 22=13 23=13 24=13 25=13 26=14 27=14 28=15 29=15 30=15

label define income 1 "Under $10,000" 2 "$10,000-$14,999" 3 "$15,000-$19,999" 4 "$20,000-$24,999" 5 "$25,000-$29,999" 6 "$30,000-$34,999" 7 "$35,000-$39,999" 8 "$40,000-$44,999" 9 "$45,000-$49,999" 9 "$50,000-$54,999" 9 "$45,000-$54,999" 10 "$55,000-$59,999" 11 "$60,000-$69,999" 12 "$70,000-$79,999" 13 "$80,000-$99,999" 14 "$100,000-$149,999" 15 "$150,000 or more"
label values income income

* Household income (quintile)

xtile income5 = income, nq(5)

* Highest income categories

gen highinc = 1 if income5 == 5
replace highinc = 0 if income5 < 5 & income!=.

label define highinc 0 "Others" 1 "Highest incomes"
label values highinc highinc

* Vote choice

gen vote_choice = fedvote

recode vote_choice 6=88 7=999 8=-99

label define vote_choice 1 "Conservative Party of Canada" 2 "Liberal Party of Canada" 3 "New Democratic Party of Canada" 4 "Bloc Quebecois" 5 "Green Party of Canada" 88 "Other party" 99 "No vote/Spoiled ballot" -999 "Don't know"
label values vote_choice vote_choice

* Vote status (district)

gen vote_district = 1 if decided == district_outcome
replace vote_district = 0 if decided!=district_outcome & decided!=. 

label define winner 0 "Voted for loser" 1 "Voted for winner"
label values vote_district winner

* Previous vote

gen voted_prev = q5

gen remember = q6

gen vote_prev = q7

recode vote_prev 7=.

gen prev = 1 if decided == vote_prev & vote_prev!=.
replace prev = 0 if decided!=vote_prev & vote_prev!=.
replace prev = 888 if remember == 2
replace prev = 999 if voted_prev == 2 | voted_prev == 3

label define prev 0 "Changed vote" 1 "Same vote" 888 "Don't remember" 999 "Did not vote/Not sure"
label values prev prev

* How certain are you that this is the party you will support?

gen certain = 5 - q4


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecast_district = q14

recode forecast_district 6=88 7=-99

label define forecast 1 "Conservative Party of Canada" 2 "Liberal Party of Canada" 3 "New Democratic Party of Canada" 4 "Bloc Québécois" 5 "Green Party of Canada" 88 "Other" -99 "Don't know"
label values forecast_district forecast

* Identify correct forecasts

gen correct_district = 0 if forecast_district!=district_outcome & forecast_district!=.
replace correct_district = 1 if forecast_district == district_outcome & forecast_district!=.

label define correct 0 "Incorrect" 1 "Correct"
label values correct_district correct


*************
** WEIGHTS **
*************

* Sex

gen sex_ca2011_w = male

recode sex_ca2011_w 0=1 1=2

label define sex_ca2011_w 1 "Female" 2 "Male"
label values sex_ca2011_w sex_ca2011_w

* Age

gen age_ca2011_w = ageval

replace age_ca2011_w = 1 if age_ca2011_w >= 18 & age_ca2011_w <= 19
replace age_ca2011_w = 2 if age_ca2011_w >= 20 & age_ca2011_w <= 24
replace age_ca2011_w = 3 if age_ca2011_w >= 25 & age_ca2011_w <= 29
replace age_ca2011_w = 4 if age_ca2011_w >= 30 & age_ca2011_w <= 34
replace age_ca2011_w = 5 if age_ca2011_w >= 35 & age_ca2011_w <= 39
replace age_ca2011_w = 6 if age_ca2011_w >= 40 & age_ca2011_w <= 44
replace age_ca2011_w = 7 if age_ca2011_w >= 45 & age_ca2011_w <= 49
replace age_ca2011_w = 8 if age_ca2011_w >= 50 & age_ca2011_w <= 54
replace age_ca2011_w = 9 if age_ca2011_w >= 55 & age_ca2011_w <= 59
replace age_ca2011_w = 10 if age_ca2011_w >= 60 & age_ca2011_w <= 64
replace age_ca2011_w = 11 if age_ca2011_w >= 65 & age_ca2011_w <= 69
replace age_ca2011_w = 12 if age_ca2011_w >= 70 & age_ca2011_w <= 74
replace age_ca2011_w = 13 if age_ca2011_w >= 75 & age_ca2011_w <= 79
replace age_ca2011_w = 14 if age_ca2011_w >= 80 & age_ca2011_w <= 84
replace age_ca2011_w = 15 if age_ca2011_w >= 85

label define age_ca2011_w 1 "18-19" 2 "20-24" 3 "25-29" 4 "30-34" 5 "35-39" 6 "40-44" 7 "45-49" 8 "50-54" 9 "55-59" 10 "60-64" 11 "65-69" 12 "70-74" 13 "75-79" 14 "80-84" 15 "85+"
label values age_ca2011_w age_ca2011_w

* Education

gen education_ca2011_w = education

recode education_ca2011_w 2=1 3=2 4=2 5=3 6=3 7=4 8=5

label define education_ca2011_w 1 "Below high school" 2 "High school" 3 "Postsecondary below bachelor" 4 "University undergraduate degree" 5 "University graduate degree"
label values education_ca2011_w education_ca2011_w

* Income

gen income_ca2011_w = income

label define income_ca2011_w 1 "Under $10,000" 2 "$10,000-$14,999" 3 "$15,000-$19,999" 4 "$20,000-$24,999" 5 "$25,000-$29,999" 6 "$30,000-$34,999" 7 "$35,000-$39,999" 8 "$40,000-$44,999" 9 "$45,000-$49,999" 9 "$50,000-$54,999" 9 "$45,000-$54,999" 10 "$55,000-$59,999" 11 "$60,000-$69,999" 12 "$70,000-$79,999" 13 "$80,000-$99,999" 14 "$100,000-$149,999" 15 "$150,000 or more"
label values income_ca2011_w income_ca2011_w

* Province

gen province_ca2011_w = province

replace province_ca2011_w = 10 if province == 1
replace province_ca2011_w = 9 if province == 2
replace province_ca2011_w = 8 if province == 3
replace province_ca2011_w = 7 if province == 4
replace province_ca2011_w = 6 if province == 5
replace province_ca2011_w = 5 if province == 6
replace province_ca2011_w = 4 if province == 7
replace province_ca2011_w = 3 if province == 8
replace province_ca2011_w = 2 if province == 9
replace province_ca2011_w = 1 if province == 10

label define province_ca2011_w 1 "Newfoundland and Labrador" 2 "Prince Edward Island" 3 "Nova Scotia" 4 "New Brunswick" 5 "Quebec" 6 "Ontario" 7 "Manitoba" 8 "Saskatchewan" 9 "Alberta" 10 "British Columbia"
label values province_ca2011_w province_ca2011_w


**********
** SAVE **
**********

save "ca2011_ipsos_invitation.dta", replace

log close